<template>
  <el-dialog title="生成链接" :visible.sync="dialogFormVisible">
    <el-form :model="form">
      <el-form-item label="品牌选择" :label-width="formLabelWidth">
        <el-select v-model="form.brand_id" placeholder="请选择品牌" @change="brandChange">
          <el-option v-for="item in brand_list" :key="item.is" :label="item.brand_name" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="级别选择" :label-width="formLabelWidth">
        <el-select v-model="form.brand_level" placeholder="请选择级别">
          <el-option v-for="item in brand_level_list" :key="item.id" :label="item.level_name" :value="item.level_int" />
        </el-select>
      </el-form-item>
      <el-form-item label="上级代理授权码" :label-width="formLabelWidth">
        <el-input v-model="form.code_num" />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="dialogFormVisible = false">取 消</el-button>
      <el-button type="primary" @click="submit">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { getBrandList, getBrandsLevels } from '@/api/brand'
import { getAgentInfoIncodeNum, addAgentEwm, getWxcode } from '@/api/agent'
export default {
  data() {
    return {
      brand_list: [],
      brand_level_list: [],
      form: {
        brand_id: '',
        brand_level: '',
        anent_id: '',
        code_num: ''
      },
      formLabelWidth: '120px',
      dialogFormVisible: false
    }
  },
  created() {
    this.getBrandList()
  },
  methods: {
    brandChange() {
      this.getLevelList()
    },
    getBrandList() {
      const data = {
        brand_id: '',
        brand_product_name: '',
        status: 1,
        num: 100000,
        p: 1
      }
      getBrandList(data).then(res => {
        console.log(res)
        this.brand_list = res.list
      })
    },
    getLevelList() {
      const data = {
        brand_id: this.form.brand_id
      }
      getBrandsLevels(data).then(res => {
        console.log(res)
        this.brand_level_list = res.list
      })
    },
    getAgentInfo() {
      const data = {
        brand_id: this.form.brand_id,
        code_num: this.form.code_num
      }
      getAgentInfoIncodeNum(data).then(res => {
        if (res.code === 200) {
          this.form.anent_id = res.log.anent_id
          const info = {
            brand_id: this.form.brand_id,
            brand_level: this.form.brand_level,
            anent_id: this.form.anent_id
          }
          addAgentEwm(info).then(res => {
            console.log(res)
            getWxcode({
              page: 'subPackages/agent/authorization/authorization',
              scene: res
            }).then(res => {
              console.log(res)
            })
          })
        } else if (res.code === 400) {
          this.$message.warning('上级授权码错误！请重新录入！')
        }
      })
    },
    submit() {
      console.log(this.form)
      this.getAgentInfo()
    }
  }
}
</script>

<style lang="sass" scoped>

</style>
